package org.fh.servcie.impl;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import org.fh.HandleData;
import org.fh.SendData;
import org.fh.cmd.CommunicationCmd;
import org.fh.common.ModeSwitchType;
import org.fh.domain.*;
import org.fh.inductionLamp.StatusHandle;
import org.fh.inductionLamp.api.LampResponse;
import org.fh.inductionLamp.impl.client.DefaultLampClient;
import org.fh.inductionLamp.impl.request.GetConfigRequest;
import org.fh.inductionLamp.impl.request.LampControlRequest;
import org.fh.inductionLamp.impl.request.ModeSwitchRequest;
import org.fh.inductionLamp.impl.response.GetConfigResponse;
import org.fh.inductionLamp.impl.response.LampControlResponse;
import org.fh.inductionLamp.impl.response.ModeSwitchResponse;
import org.fh.servcie.DataService;
import org.fh.utils.ConvertCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wwy
 * @Date: 2020/10/19 16:15
 */
@Service
public class DataServiceImpl implements DataService {
    private static final Logger LOGGER = LoggerFactory.getLogger(DataServiceImpl.class);
    public static GetConfigResponse getConfigResponse;


    /**
     * 获取配置
     */
    @Override
    public GetConfigResponse getConfig(int srcId,int destId){
        DefaultLampClient defaultLampClient = new DefaultLampClient(srcId,destId);
        GetConfigRequest getConfigRequest = new GetConfigRequest();
        getConfigResponse = defaultLampClient.execute(getConfigRequest);
        LOGGER.info("获取参数配置：{}",getConfigResponse.toString());
        return getConfigResponse;
    }

    /**
     * 获取全部在线配置信息
     * @param srcId
     * @return
     */
    @Override
    public List<GetConfigResponse> getConfigAll(int srcId) {
        List<GetConfigResponse> returnList = new ArrayList<>();
        if(HandleData.connectList == null){
            return returnList;
        }
        for (String s : HandleData.connectList) {
            GetConfigResponse config = getConfig(srcId, Integer.parseInt(s));
            returnList.add(config);
        }
        return returnList;
    }

    /**
     * 更改模式
     * @return
     */
    @Override
    public ModeSwitchResponse modeSwitch(ModeSwitch modeSwitch) {
        int model = modeSwitch.getModel();
        DefaultLampClient defaultLampClient = new DefaultLampClient(modeSwitch.getSrcId(),modeSwitch.getDestId());
        ModeSwitchRequest modeSwitchRequest = new ModeSwitchRequest();
        List<String> pData = new ArrayList<>();
        if(model == ModeSwitchType.AUTO){
            pData.add("AA");
        }else if(model == ModeSwitchType.MANUAL){
            pData.add("55");
        }else{
            return new ModeSwitchResponse("");
        }

        modeSwitchRequest.setpData(pData);
        modeSwitchRequest.setLength(pData.size());
        ModeSwitchResponse response = defaultLampClient.execute(modeSwitchRequest);
        LOGGER.info("获取参数配置：{}",response.toString());
        return response;
    }

    /**
     * 灯控制
     * @param lampControl
     * @return
     */
    @Override
    public LampControlResponse lampController(LampControl lampControl){
        DefaultLampClient defaultLampClient = new DefaultLampClient(lampControl.getSrcId(),lampControl.getDestId());
        LampControlRequest lampControlRequest = new LampControlRequest();
        List<String> pData = new ArrayList<>();


        String controllerStartId = intConvert4(Integer.parseInt(lampControl.getControllerStartId()),true);
        String controllerStartIdScope1 = intConvert4(Integer.parseInt(lampControl.getControllerStartIdScope1()), true);
        String controllerEndIdScope1 = intConvert4(Integer.parseInt(lampControl.getControllerEndIdScope1()), true);
        String controllerStartIdScope2 = intConvert4(Integer.parseInt(lampControl.getControllerStartIdScope2()), true);
        String controllerEndIdScope2 = intConvert4(Integer.parseInt(lampControl.getControllerEndIdScope2()), true);
        String deliveryMode = intConvert2(Integer.parseInt(lampControl.getDeliveryMode()));
        String deliveryIntervalNumber = intConvert2(Integer.parseInt(lampControl.getDeliveryIntervalNumber()));
        String controllerSource = intConvert2(Integer.parseInt(lampControl.getControllerSource()));
        String yellowLightFrequency = intConvert2(Integer.parseInt(lampControl.getYellowLightFrequency()));
        String yellowLightLuminance = intConvert2(Integer.parseInt(lampControl.getYellowLightLuminance()));
        String redLightFrequency = intConvert2(Integer.parseInt(lampControl.getRedLightFrequency()));
        String redLightLuminance = intConvert2(Integer.parseInt(lampControl.getRedLightLuminance()));
        String wakeTime = intConvert2(Integer.parseInt(lampControl.getWakeTime()));
        String wakeArray = intConvert2(Integer.parseInt(lampControl.getWakeArray()));
        String carInspectionDeviceStatus = intConvert2(Integer.parseInt(lampControl.getCarInspectionDeviceStatus()));
        String spikeStatus = intConvert2(Integer.parseInt(lampControl.getSpikeStatus()));

        pData.add(controllerStartId.substring(0,2));
        pData.add(controllerStartId.substring(2,4));
        pData.add(controllerStartIdScope1.substring(0,2));
        pData.add(controllerStartIdScope1.substring(2,4));
        pData.add(controllerEndIdScope1.substring(0,2));
        pData.add(controllerEndIdScope1.substring(2,4));
        pData.add(controllerStartIdScope2.substring(0,2));
        pData.add(controllerStartIdScope2.substring(2,4));
        pData.add(controllerEndIdScope2.substring(0,2));
        pData.add(controllerEndIdScope2.substring(2,4));
        pData.add(deliveryMode);
        pData.add(deliveryIntervalNumber);
        pData.add(controllerSource);
        pData.add(yellowLightFrequency);
        pData.add(yellowLightLuminance);
        pData.add(redLightFrequency);
        pData.add(redLightLuminance);
        pData.add(wakeTime);
        pData.add(wakeArray);
        pData.add(spikeStatus);
        pData.add(carInspectionDeviceStatus);
        pData.add("00");
        pData.add("00");
        pData.add("00");
        pData.add("00");
        pData.add("00");
        pData.add("00");
        pData.add("00");
        pData.add("03");
        pData.add("00");
        pData.add("00");
        pData.add("00");
        lampControlRequest.setpDSata(pData);
        lampControlRequest.setLength(pData.size());
        LampControlResponse lampControlResponse = defaultLampClient.execute(lampControlRequest);
        return lampControlResponse;
    }

    /**
     * 获取设备状态
     * @return
     */
    @Override
    public LampStatus getLampStatus() {
        LampStatus lampStatus = StatusHandle.lampStatus;
        if(lampStatus == null){
            lampStatus = new LampStatus();
        }
        return lampStatus;
    }

    public String intConvert4(int value, boolean isLowHighConvert){
        String hexString = Integer.toHexString(value);
        int hexLength = hexString.length();
        if(hexLength<4){
            for(int i=0;i<4-hexLength;i++){
                hexString = "0" + hexString;
            }
        }

        if(isLowHighConvert){
            return hexString.substring(2,4)+hexString.substring(0,2);
        }else{
            return hexString;
        }
    }

    public String intConvert2(int value){
        String hexString = Integer.toHexString(value);
        int hexLength = hexString.length();
        if(hexLength<4){
            for(int i=0;i<2-hexLength;i++){
                hexString = "0" + hexString;
            }
        }
        return hexString;
    }

}
